package so.ifl.manager;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;

import so.ifl.servcie.CompanyService;
import so.ifl.vo.CompanyVo;

/**
 * create by aaron.lu create at 2011-12-19 上午10:49:34
 * <mailto:atyuan.lu@gmail.com>
 **/
@Component("companyManager")
public class CompanyManager
{

	@Autowired
	private CompanyService companyService;
	
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	
	/**
	 * 
	 * create by aaron.lu create at 2011-12-19上午3:19:23 <mailto:
	 * atyuan.lu@gmail.com>
	 * 
	 * @param page
	 * @return
	 * 
	 */
	@DataProvider
	public void getCompany(Page<CompanyVo> page, Map<String, Object> map) {
		logger_.debug("params:" + map);
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		List<CompanyVo> list = companyService.queryCompanyWithParamAndPagination(map, offset, limmit);

		int countsql = companyService.countCompanyWithParam(map);
		page.setEntities(list);
		page.setEntityCount(countsql);
	}

	@DataResolver
	public void updateCompany(Collection<CompanyVo> companys) {
		for (CompanyVo company : companys) {
			EntityState entityState = EntityUtils.getState(company);
			if (EntityState.NEW.equals(entityState)) {
				//新增
				insertCompany(company);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				//修改
				updateCompany(company);
			} else if (EntityState.DELETED.equals(entityState)) {
				//删除
				deleteCompany(company.getCompanyId());
			}
		}
	}
	/**
	 * 新增公司信息
	 * create by simon
	 * create at 2011-12-22 上午10:11:56
	 * <mailto:liangyuanliu@gmail.com>
	 * @param companyVo
	 */
	private void insertCompany(CompanyVo companyVo)
	{		
		Map<String,Object> m = utilMap(companyVo,false);
		companyService.insertCompany(m);
	}
	/**
	 * 更新公司信息
	 * create by simon
	 * create at 2011-12-22 上午10:12:58
	 * <mailto:liangyuanliu@gmail.com>
	 * @param companyVo
	 */
	private void updateCompany(CompanyVo companyVo)
	{
		Map<String,Object> m = utilMap(companyVo,true);
		companyService.updateCompany(m);
	}
	/**
	 * 删除公司信息
	 * create by simon
	 * create at 2011-12-22 上午10:13:47
	 * <mailto:liangyuanliu@gmail.com>
	 * @param id
	 */
	private void deleteCompany(int id)
	{
		companyService.deleteCompany(id);
	}
	
	private Map<String,Object> utilMap(CompanyVo companyVo,boolean update)
	{
		Map<String,Object> m =new HashMap<String, Object>();
		if(update)
		{
			m.put("company_id", companyVo.getCompanyId());
		}
		
		m.put("company_bm_", companyVo.getCompanyBm());
		m.put("company_name_", companyVo.getCompanyName());
		m.put("company_address_", companyVo.getCompanyAddress());
		m.put("company_linkman_", companyVo.getCompanyLinkman());
		m.put("company_contact_", companyVo.getCompanyContact());
		m.put("company_tel_", companyVo.getCompanyTel());
		m.put("company_email_", companyVo.getCompanyEmail());
		m.put("is_do_", companyVo.getIsDo());
		m.put("company_active_date_", companyVo.getCompanyActiveDate());
		m.put("allow_studuent_", companyVo.getAllowStudent());
		m.put("company_ext_", companyVo.getCompanyExt());
		m.put("company_abridge_", companyVo.getCompanyAbridge());
		return m;
	}
	

}
